//
//  LCWEmotionToolBar.swift
//  MyWB
//
//  Created by 李成万 on 16/7/4.
//  Copyright © 2016年 李成万. All rights reserved.
//

import UIKit
let tempEnum = 1000
enum emotionToolBarType : Int{
    case Default = 0
    case Emoji = 1
    case LXH = 2
}

class LCWEmotionToolBar: UIView {

    var currentBtn : UIButton = UIButton(type: UIButtonType.Custom)
    
    var emotionToolBarBTnClickClosure:((type:emotionToolBarType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        let leftBtn = addButton("left", title: "默认",type: .Default)
        let midBtn = addButton("mid", title: "Emoji",type: .Emoji)
        let rightBtn = addButton("right", title: "浪小花",type: .LXH)
        
        leftBtn.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(midBtn)
        }
        midBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(leftBtn.snp_trailing)
            make.width.equalTo(rightBtn)
        }
        rightBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(midBtn.snp_trailing)
        
        }
        
        leftBtn.selected = true
        currentBtn = leftBtn
        
    }
    
    
    func addButton(imageName:String,title:String,type:emotionToolBarType)->UIButton{
        let btn = UIButton(type: UIButtonType.Custom)
        
        
        //compose_emotion_table_left_normal
        //compose_emotion_table_left_selected
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_selected"), forState: .Selected)
        
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        
        
        btn.tag = type.rawValue + tempEnum
        
        btn.addTarget(self, action: "toolBarBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        addSubview(btn)
        return btn;
    }
    
    func setCurrentButton(tag:Int){
        let btn = viewWithTag(tag + tempEnum) as! UIButton
        
        if currentBtn == btn{
            return
        }
        
        btn.selected = true
        currentBtn.selected = false
        currentBtn = btn
    }
    
    
    func toolBarBtnClick(btn:UIButton){
        
        if currentBtn == btn{
            return
        }
        
        btn.selected = true
        currentBtn.selected = false
        currentBtn = btn
        
        emotionToolBarBTnClickClosure?(type:emotionToolBarType(rawValue: btn.tag - tempEnum)!)
    }
}
